/**
 *
 * @author ace
 * @create 2017/7/27
 */
class DaikaiPlayerItem extends ComponentEx {

    head: HeadIcon;
    lab_nick: eui.Label;
    lab_state: eui.Label;
    lab_score: eui.Label;
    btn_kill: eui.Button;

    isStarting: boolean = false;

    constructor() {
        super();
        this.skinName = "DaikaiPlayerItemSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.head.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_kill.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.head:
            case this.btn_kill:
                if (this.isStarting || this.skinState != 'status') return;

                if (this.data) {
                    let thiz = this;
                    game.askPanel.showMsg(function (r: boolean) {
                        if (r) {
                            gameManager.socketManager.send(33, {
                                roomid: thiz.data.roomid,
                                pos: thiz.data.pos
                            });
                        }
                    }, `是否将玩家【${this.data.nick}】踢出本房间？`);
                }
                break;
        }
    }

    update(data: any) {
        this.clean();

        if (!data) return;

        this.data = data;

        if (!this.data.hasOwnProperty('cur')) this.skinState = 'status';
        else this.skinState = 'score';

        this.head.src = data.pic;
        this.head.isVip = data.hasOwnProperty('gid');
        this.lab_nick.text = data.nick;

        this.lab_state.text = data.status == 'online' ? '(在线)' : '(离线)';
        this.lab_state.textColor = data.status == 'online' ? 0x239262 : 0xff0408;

        this.lab_score.text = '' + data.cur;
        this.lab_score.textColor = data.cur > 0 ? 0xdd393b : 0x3c9cde;

        this.starting = this.data.status == 'start';
    }

    empty() {
        this.skinState = 'empty';

        this.head.update(null);
    }

    lock() {
        this.skinState = 'lock';
    }

    clean() {
        this.head.src = '';
        this.lab_nick.text = '';
        this.lab_state.text = '';
        this.lab_score.text = '';
        this.btn_kill.visible = false;

        this.lock();
    }

    set starting(v: boolean) {
        if (!this.data) return;

        this.isStarting = v;
        this.btn_kill.visible = !v;
    }
}